/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.data.formatter.NumberBaseRoundType;
import fever.data.formatter.NumberFormatter;

/**
 * {@code BytesFormatter } class formats a bytes value adjusting the decimal 
 * rounding, precision, the thousands separator character and the unity label.
 * 
 * @author Romain Ecarnot
 */
class fever.data.formatter.BytesFormatter extends NumberFormatter
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var BYTE : Number = 8;
	private static var KILO_BYTE : Number = 8192;
	private static var MEGA_BYTE : Number = 8388608;
	private static var GIGA_BYTE : Number = 8589934592;
	private static var TERA_BYTE : Number = 8796093022208;
	
	private static var BYTE_UNITY : String = ' o';
	private static var KILO_BYTE_UNITY : String = ' Ko';
	private static var MEGA_BYTE_UNITY : String = ' Mo';
	private static var GIGA_BYTE_UNITY: String = ' Go';
	private static var TERA_BYTE_UNITY : String = ' To';
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
		
	/**
	 * Constructor.
	 */ 
	public function BytesFormatter() 
	{
		super();
		
		precision = 0;
		rounding = NumberBaseRoundType.NEAREST;
	}
	
	/**
	 * Formats and returns passed-in {@code value}.
	 * 
	 * @return Formatted {@code String}
	 */
	public function format( bytes : Number ) : String
	{
		var result : String;
		
		 //bytes -> bits
		bytes *= BYTE;
		
		if( bytes < KILO_BYTE )
		{
			result = super.format( bytes / BYTE ) + BYTE_UNITY;
		}
		else if( bytes < MEGA_BYTE )
		{
			result = super.format( bytes / KILO_BYTE ) + KILO_BYTE_UNITY;
		}
		else if( bytes < GIGA_BYTE )
		{
			result = super.format( bytes / MEGA_BYTE ) + MEGA_BYTE_UNITY;
		}
		else if( bytes < TERA_BYTE )
		{
			result = super.format( bytes / GIGA_BYTE ) + GIGA_BYTE_UNITY;
		}
		else
		{
			result = super.format( bytes / TERA_BYTE ) + TERA_BYTE_UNITY;
		}
		
		return result;
	}
}